package engine.newUi
{
	import engine.control.KitaWebServices;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.flag.ResourcesPath;
	import engine.logic.event.LogicEvent;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.ui.lite.BaseUI;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.coders.MD5;
	
	import toolNet.Model;
	
	/**
	 * 手机验证
	 * @author wangzj
	 * 
	 */	
	public class MobileCheckLogic
	{
		private static var _self	:MobileCheckLogic; //单例
		private static var m_MobildCheck:MobileCheckUI;
		
		private var m_accessType:int = 1;
		private var m_CheckString:String;
		private var m_accessId:int;
		private var m_checkSuccess:Boolean;
		private var m_SaveNumber:String;
		private var m_SaveCheck:String;
		private var m_StartShow:Boolean;
		public function MobileCheckLogic()
		{
			
		}
		/**
		 *  单例
		 */		
		public static function getInstance():MobileCheckLogic
		{
			if(_self == null)
			{
				_self = new MobileCheckLogic();
			}
			return _self;
		}
		/**
		 * 初始化界面
		 * 初始换所有的数据
		 */		
		public function Init():void
		{
			m_MobildCheck = MobileCheckUI.getInstance();
			
		}
		
		/**
		 * 显示界面
		 * 
		 */		
		public function BeforeShow():void
		{
			m_MobildCheck.layout = BaseUI.CENTER;
			addEvent();        //监听事件
			m_accessType = 1;
			UISet();
		}
		
		/**
		 * 关闭界面
		 * 
		 */		
		public function AfterHide():void
		{
			removeEvent();
			m_accessType = 0;
			m_checkSuccess = false;
		}
		
		/**
		 * 监听事件
		 * 
		 */		
		private function addEvent():void
		{
			if(!SceneManager._itemIconLoad)
			{
				ModelLocator.getInstance().addEventListener(LogicEvent.ITEMICONLOADED,loaded);
				function loaded():void
				{
					var DP:DisplayObject = ResManager.newResClass(ResourcesPath.getPropsURL(int(45003005)));
					m_MobildCheck.SuperManGift.source = DP;
					ModelLocator.getInstance().removeEventListener(LogicEvent.ITEMICONLOADED,loaded)
				} 
			}
			m_MobildCheck.MobileBtn.addEventListener(MouseEvent.CLICK , OnMobileClick );
			m_MobildCheck.CheckBtn.addEventListener(MouseEvent.CLICK , OnCheckClick );
			m_MobildCheck.MobileNumber.addEventListener(MouseEvent.CLICK , OnNumberClick);
			m_MobildCheck.CheckNumber.addEventListener(MouseEvent.CLICK , OnCheckLClick);
			m_MobildCheck.MobileNumber.addEventListener(TextEvent.TEXT_INPUT,OnMobileInput);
			m_MobildCheck.CheckNumber.addEventListener(TextEvent.TEXT_INPUT,OnCheckInput);
			m_MobildCheck.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClickCloseHandle);
			m_MobildCheck.gotoGift.addEventListener(MouseEvent.CLICK,onGotoTouch);
		}
		
		/**
		 * 移除监听事件
		 * 
		 */		
		private function removeEvent():void
		{
			
			
			m_MobildCheck.MobileBtn.removeEventListener(MouseEvent.CLICK , OnMobileClick );
			m_MobildCheck.CheckBtn.removeEventListener(MouseEvent.CLICK , OnCheckClick );
			m_MobildCheck.MobileNumber.removeEventListener(MouseEvent.CLICK , OnNumberClick);
			m_MobildCheck.CheckNumber.removeEventListener(MouseEvent.CLICK , OnCheckLClick);
			m_MobildCheck.MobileNumber.removeEventListener(TextEvent.TEXT_INPUT,OnMobileInput);
			m_MobildCheck.CheckNumber.removeEventListener(TextEvent.TEXT_INPUT,OnCheckInput);
			m_MobildCheck.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClickCloseHandle);
			m_MobildCheck.gotoGift.removeEventListener(MouseEvent.CLICK,onGotoTouch);
		}
		
		
		/**
		 * 初始化界面设置
		 * 
		 */		
		private function UISet():void
		{
			m_MobildCheck.gift.htmlText     = StaticIni.getSwfLangStr("UIString_1000612");
			m_MobildCheck.giftRule.htmlText = StaticIni.getSwfLangStr("UIString_1000611");
			if( m_SaveCheck ){
				m_MobildCheck.CheckNumber.text = m_SaveCheck;
			}else{
				m_MobildCheck.CheckNumber.text  = StaticIni.getSwfLangStr("UIString_1000652");
			}
			if(m_SaveNumber ){
				m_MobildCheck.MobileNumber.text = m_SaveNumber;
			}else{
				m_MobildCheck.MobileNumber.text = StaticIni.getSwfLangStr("UIString_1000653");
			}
			m_MobildCheck.MobileNumber.multiline = false;
			m_MobildCheck.CheckNumber.multiline = false;
			m_MobildCheck.MobileNumber.maxChars = 11;
			m_MobildCheck.MobileNumber.restrict = "0-9";
			m_MobildCheck.CheckNumber.restrict  = "a-z0-9A-Z";
			
			m_MobildCheck.SuperManLabel.text = StaticIni.getSwfLangStr("UIString_1000654");
			m_MobildCheck.Label1.text        = StaticIni.getSwfLangStr("UIString_1000613");
			
			m_MobildCheck.gotoGift.label = StaticIni.getSwfLangStr("UIString_1000656");
			m_MobildCheck.MobileBtn.label = StaticIni.getSwfLangStr("UIString_1000655");
			
			var DP:DisplayObject = ResManager.newResClass(ResourcesPath.getPropsURL(int(45003005)));
			m_MobildCheck.SuperManGift.source = DP;
		}
		
		private function onClickCloseHandle( event:NxWindowEvent ) : void
		{
			m_SaveNumber = m_MobildCheck.MobileNumber.text;
			m_SaveCheck   = m_MobildCheck.CheckNumber.text;
			if(m_SaveNumber.length != 11)m_SaveNumber = "";
			if(m_SaveCheck.length != 6)m_SaveCheck = "";
			
			m_MobildCheck.hide();
		}
		
		private function OnNumberClick( event:MouseEvent ) : void
		{
			m_MobildCheck.MobileNumber.text = "";
			if(m_MobildCheck.CheckNumber.text == "")
			{
				m_MobildCheck.CheckNumber.text = StaticIni.getSwfLangStr("UIString_1000652");
			}
		}
		private function OnCheckLClick( event:MouseEvent ) : void
		{
			m_MobildCheck.CheckNumber.text = "";
			if(m_MobildCheck.MobileNumber.text == "")
			{
				m_MobildCheck.MobileNumber.text = StaticIni.getSwfLangStr("UIString_1000653");
			}
		}
		
		private function onGotoTouch( event:MouseEvent ) : void
		{
			MessageUI.getInstance().showInfo(  StaticIni.getSwfLangStr("UIString_1000619") );
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PLEASE_DRAW_MOBILE_GIFT,
				"",
				"");
		}
		/**
		 * 检测手机是否已认证
		 * 
		 */		
		private var checkStart:Boolean;
		public function CheckNumberisAgree( access:int ):void
		{
			if( !m_MobildCheck.created ){
				checkStart = true;
				m_StartShow = true;
				m_accessType = 2;    
				m_accessId = access;
				m_CheckString = "123123";
				m_MobildCheck.MobileNumber.text = "12345678912";
				SendWebService();
			}else{
				onClickCloseHandle(null);
				m_MobildCheck.hide();
			}
		}
		
		/**       
		 * 发送手机号码
		 * @param accessId
		 * @param accessPasswd
		 * @param accessType
		 * @param type 1-发送短信,2-手机校验
		 * @param aid 帐号id
		 * @param mobile 手机号
		 * @param validateCode 手机短信校验码 
		 * @param returnType 返回类型,传json或xml
		 * @param sVerifyStr 校验窜(把以上参数拼起来,在拼上key,md5转大写)
		 */	
		private function OnMobileClick(event:MouseEvent):void
		{
			if(m_MobildCheck.MobileNumber.text == ""||
				m_MobildCheck.MobileNumber.text == StaticIni.getSwfLangStr("UIString_1000653")){
				MessageUI.getInstance().showInfo(  StaticIni.getSwfLangStr("UIString_1000617") );
				return;
			}
			
			if(m_MobildCheck.MobileNumber.text.length < 11){
				MessageUI.getInstance().showInfo(  StaticIni.getSwfLangStr("UIString_1000616") );
				return;
			}
			
			m_accessType = 1;        
			m_CheckString = "";
			SendWebService();
			m_StartShow = false;
			m_SaveNumber = m_MobildCheck.MobileNumber.text;
			MessageUI.getInstance().showInfo(  StaticIni.getSwfLangStr("UIString_1000619") );
		}
		private function mobileReturn( xml:XML ):void
		{
			var value:XMLList=xml.elements().elements().elements();
			var strrrrr:String=value.toString();
			var xml:XML = XML(strrrrr);
			var result:int = int(xml.@msgcode);
			var message:String = xml.@message.toString();
			
			if(m_StartShow == false){
				if(result != 1){
					MessageUI.getInstance().showInfo( message );
				}
			}
			if( m_accessType == 2&&checkStart == false){
				if( result!= 1 ){
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PLEASE_DRAW_MOBILE_GIFT,
						"",
						"");
					return;
				}
				
				if( m_MobildCheck.MobileNumber.text&&
					m_MobildCheck.MobileNumber.text!= StaticIni.getSwfLangStr("UIString_1000653") )
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PLEASE_DRAW_MOBILE_GIFT,
						m_MobildCheck.MobileNumber.text,
						m_MobildCheck.CheckNumber.text);
				}
				
				m_checkSuccess = true;
			}
			
			if( m_accessType == 2&&checkStart == true){
				if( result == 54067 ){
					MobileCheckUI.getInstance().showOrHide();
					MobileCheckUI.getInstance().CheckBtn.visible = false;
					MobileCheckUI.getInstance().MobileBtn.enabled = false;
					MobileCheckUI.getInstance().MobileNumber.editable = false;
					MobileCheckUI.getInstance().CheckNumber.editable = false;
					MobileCheckUI.getInstance().gotoGift.visible = true;
				}else{
					MobileCheckUI.getInstance().showOrHide();
					MobileCheckUI.getInstance().CheckBtn.visible = true;
					MobileCheckUI.getInstance().MobileBtn.enabled = true;
					MobileCheckUI.getInstance().MobileNumber.editable = true;
					MobileCheckUI.getInstance().CheckNumber.editable = true;
					MobileCheckUI.getInstance().gotoGift.visible = false;
				}
				checkStart = false;
			}
		}
		
		private function SendWebService() : void
		{
			//串
			var array:Array =ModelLocator.getInstance().mainIni.getContent("check","accessid").split(",");
			//地址
			var str:String  = ModelLocator.getInstance().mainIni.getContent("check","url");
			//函数接口
			var str_1:String  = ModelLocator.getInstance().mainIni.getContent("check","accesstype");
			
			var webService:KitaWebServices = new KitaWebServices( str );
			
			var fixStr:String=MD5.hash((
				array[0].toString() +
				array[1].toString()+
				array[2].toString()
				+m_accessType.toString()
				+m_accessId
				+m_MobildCheck.MobileNumber.text.toString()
				+m_CheckString
				+"xml"
				+array[3].toString())).toLocaleUpperCase();
			
			webService.call(str_1, 
				mobileReturn,    
				array[0].toString(),            
				array[1].toString(),
				array[2].toString(),             
				m_accessType.toString(),             
				m_accessId,             
				m_MobildCheck.MobileNumber.text.toString(),
				m_CheckString,             
				"xml",          
				fixStr);   
		}
		
		/**
		 * 验证按钮 
		 * @param event
		 * 
		 */		
		private function OnCheckClick(event:MouseEvent):void
		{
			if(m_MobildCheck.CheckNumber.text == StaticIni.getSwfLangStr("UIString_1000652")||
				m_MobildCheck.CheckNumber.text == ""){
				MessageUI.getInstance().showInfo(  StaticIni.getSwfLangStr("UIString_1000618") );
				return;
			}
			if(m_MobildCheck.MobileNumber.text == ""||
				m_MobildCheck.MobileNumber.text ==StaticIni.getSwfLangStr("UIString_1000653")){
				MessageUI.getInstance().showInfo( StaticIni.getSwfLangStr("UIString_1000617") );
				return;
			}
			
			if(m_MobildCheck.MobileNumber.text.length < 11){
				MessageUI.getInstance().showInfo( StaticIni.getSwfLangStr("UIString_1000616") );
				return;
			}
			if(m_MobildCheck.CheckNumber.text.length < 6){
				MessageUI.getInstance().showInfo(  StaticIni.getSwfLangStr("UIString_1000618") );
				return;
			}
			
			if( !m_checkSuccess ){
				m_accessType = 2;
				m_StartShow = false;
				m_CheckString = m_MobildCheck.CheckNumber.text;
				m_SaveCheck   = m_MobildCheck.CheckNumber.text;
				SendWebService();
				MessageUI.getInstance().showInfo(  StaticIni.getSwfLangStr("UIString_1000619") );
			}else{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PLEASE_DRAW_MOBILE_GIFT,
					"",
					"");
				MessageUI.getInstance().showInfo(  StaticIni.getSwfLangStr("UIString_1000619") );
			}
		}
		/**
		 * 手机input
		 * @param event
		 * 
		 */		
		private function OnMobileInput( event:TextEvent ) : void
		{
			if(m_MobildCheck.MobileNumber.text == StaticIni.getSwfLangStr("UIString_1000653")){
				m_MobildCheck.MobileNumber.text = "";
			}
		}
		/**
		 * 验证吗input
		 * @param event
		 * 
		 */		
		private function OnCheckInput( event:TextEvent ) : void
		{
			if(m_MobildCheck.CheckNumber.text == StaticIni.getSwfLangStr("UIString_1000652")){
				m_MobildCheck.CheckNumber.text = "";
			}
		}
		
		
		
	}
}